package mtn.bowen.yan.server.service.impl;

import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXRadioButton;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import mtn.bowen.yan.server.common.Constant;
import mtn.bowen.yan.server.dao.PictureOfDishesDao;
import mtn.bowen.yan.server.fx.StageManager;
import mtn.bowen.yan.server.model.PictureDishes;
import mtn.bowen.yan.server.service.JavaFxUiService;
import mtn.bowen.yan.server.service.PictureDishesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author <a href=mailto:yanbowen90@foxmail.com> bowen.yan </a>
 * @version 1.0
 * @description: TODO
 * @date 2021-10-29 1:57 PM
 */
@Component
public class PictureDishesServiceImpl implements PictureDishesService {

    private final static Logger LOGGER = LoggerFactory.getLogger(PictureDishesServiceImpl.class);

    @Resource
    private JavaFxUiService javaFxUiService;

    @Resource
    private PictureOfDishesDao pictureOfDishesDao;

    @Override
    public void savePictureDishes(PictureDishes pictureDishes) {

        pictureOfDishesDao.insertPictureDishes(pictureDishes);
    }

    /**
     * 按照kind获取所有菜品种类
     *
     * @param kind 种类代码
     * @return 所有kind集合
     */
    @Override
    public List<PictureDishes> getPictureDishesByKind(int kind) {

        return pictureOfDishesDao.selectByKind(kind);
    }

    /**
     * 获取菜品行数
     *
     * @param all 所有菜品
     * @param col 菜品列数
     * @return 计算所得行数
     */
    @Override
    public int getRow(int all, int col) {
        return all % col == 0 ? all / col : all / col + 1;
    }

    /**
     * 按种类显示全部菜品
     */
    @Override
    public VBox deleteRecipe(JFXComboBox deleteRecipeComboBox) {

        int indexKind = deleteRecipeComboBox.getSelectionModel().getSelectedIndex();
        List<PictureDishes> pictureDishesByKind = getPictureDishesByKind(indexKind + 1);
        int allRow = getRow(pictureDishesByKind.size(), Constant.DELETE_RECIPE_COLUMN);
        int tempRow = allRow;
        VBox allVBox = new VBox();
        try {
            int count = 0;
            int flag = 0;
            for (int row = 0; row < allRow; row++) {
                HBox hBox = javaFxUiService.getPictureHBox(Pos.CENTER_LEFT, new Insets(10, 10, 10, 10));
                hBox.setSpacing(60);
                if (tempRow >= 2) {
                    for (int col = 0; col < Constant.DELETE_RECIPE_COLUMN; col++) {
                        PictureDishes pictureDishes = pictureDishesByKind.get(flag);
                        VBox vBox = javaFxUiService.getVBox(new Insets(10, 10, 10, 10));
                        ImageView view = javaFxUiService.getImageView(150.0, 150.0, "/picture/" + pictureDishes.getUuid());
                        JFXRadioButton jButton = new JFXRadioButton(pictureDishes.getName());
                        jButton.setStyle("-fx-font-family: STKaiTi;-fx-border-width: 1px;-fx-font-size: 22;");
                        vBox.getChildren().add(view);
                        vBox.getChildren().add(jButton);
                        //设置标志位 set in Map
                        jButton.setId(pictureDishes.getUuid());
                        jButton.setOnAction(event -> {
                            StageManager.deleteRecipeMapDropIfExists(jButton.getId());
                        });
                        hBox.getChildren().add(vBox);
                        count++;
                        flag++;
                    }
                    tempRow--;
                    allVBox.getChildren().add(hBox);
                } else {
                    for (int col = 0; col < pictureDishesByKind.size() - count; col++) {
                        PictureDishes pictureDishes = pictureDishesByKind.get(flag);
                        VBox vBox = javaFxUiService.getVBox(new Insets(10, 10, 10, 10));
                        ImageView view = javaFxUiService.getImageView(150.0, 150.0, "/picture/" + pictureDishes.getUuid());
                        JFXRadioButton jButton = new JFXRadioButton(pictureDishes.getName());
                        jButton.setStyle("-fx-font-family: STKaiTi;-fx-border-width: 1px;-fx-font-size: 22;");
                        vBox.getChildren().add(view);
                        vBox.getChildren().add(jButton);
                        //
                        //设置标志位 set in Map
                        jButton.setId(pictureDishes.getUuid());
                        jButton.setOnAction(event -> {
                            StageManager.deleteRecipeMapDropIfExists(jButton.getId());
                        });
                        hBox.getChildren().add(vBox);
                        flag++;
                    }
                    allVBox.getChildren().add(hBox);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return allVBox;
    }

    /**
     * 删除菜品更新数据
     */
    @Override
    public void deletePicture() {

        Map<String, String> deleteRecipeMap = StageManager.getDeleteRecipeMap();

        if (deleteRecipeMap.size() > 0) {
            for (String key : deleteRecipeMap.keySet()) {
                pictureOfDishesDao.deleteByUUid(key);
            }
        }
        deleteRecipeMap.clear();
    }

    @Override
    public PictureDishes selectPictureDishesByUUid(String uuid) {

        PictureDishes dishes = null;
        try {
            dishes = pictureOfDishesDao.selectByUUid(uuid);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("PictureDishesServiceImpl.selectPictureDishesByUUid is error", e);
        }
        return dishes;
    }
}
